package com.tfj.wxapi;

import android.os.Bundle;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.hyphenate.tfj.R;
import com.tfj.DevMvpApplication;
import com.tfj.mvp.base.BaseActivity;
import com.tfj.utils.AuthPreferences;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

/**
 * Created by xiaoqiang on 2018/1/4.
 */

public class WXEntryActivity extends BaseActivity<PWeChatImpl> implements IWXAPIEventHandler, CWeChat.IVWeChat {
    private static final int RETURN_MSG_TYPE_LOGIN = 1;
    private static final int RETURN_MSG_TYPE_SHARE = 2;
    private String code = "";
    private String TAG = "WXEntryActivity_";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        DevMvpApplication.mWxApi.handleIntent(getIntent(), this);
        Log.i(TAG, "WXEntryActivity--->onCreate");
    }

    @Override
    public int setContentViewId() {
        ifNeedStatus = false;
        return R.layout.fragment_rank_list;
    }

    @Override
    public void createPresenter() {
        mPresenter = new PWeChatImpl(mContext, this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "WXEntryActivity--->onDestroy");
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp resp) {

        Log.i(TAG, "WXEntryActivity--->" + (resp.errCode));
        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                Toast.makeText(WXEntryActivity.this, "拒绝授权微信登录", Toast.LENGTH_SHORT).show();
                this.finish();
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                if (RETURN_MSG_TYPE_SHARE == resp.getType()) {
                    Toast.makeText(WXEntryActivity.this, "取消了微信分享", Toast.LENGTH_SHORT).show();
                    this.finish();
                } else {
                    Toast.makeText(WXEntryActivity.this, "取消了微信登录", Toast.LENGTH_SHORT).show();
                    this.finish();
                }
                break;
            case BaseResp.ErrCode.ERR_OK:
                switch (resp.getType()) {
                    case RETURN_MSG_TYPE_LOGIN:
                        //拿到了微信返回的code,立马再去请求access_token
                        String code = ((SendAuth.Resp) resp).code;
                        String ifBind = AuthPreferences.getBind();
                        if(TextUtils.isEmpty(ifBind)){
                            mPresenter.wechat_login(code);
                        }else if(ifBind.equals("1")){
                            mPresenter.bindPhone("2","","","",code);
                        }
                        break;
                    case RETURN_MSG_TYPE_SHARE:
                        Toast.makeText(WXEntryActivity.this, "微信分享成功", Toast.LENGTH_SHORT).show();
                        finish();
                        break;
                }
                break;
        }
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void callBackLoginWechat(int code, JSONObject jsonObject, String message) {
        if (code == 1) {
            String userSring =jsonObject.toJSONString();
            AuthPreferences.saveUserToken(userSring);
           // ActivityManagerUtils.getInstance().finishActivity(VLogActivity.class);
        } else {
            showToast(message);
        }
        this.finish();
    }

    @Override
    public void callBackBind(int code, JSONObject jsonObject, String message) {
        AuthPreferences.saveBind("");
        if(code==1){
        }
        this.finish();
        showToast(message);
    }

    /**
     * //     * 携带参数获取用户信息
     * //     *
     * //     * @param code1
     * //
     */
//    private void getToken(String code1) {
//        code = code1;
//        String  url =  "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+Constant.WX_APP_ID+"&secret="+Constant.WX_APP_SECRET+"&code=" + code + "&grant_type=authorization_code";
//        OkhttpUtil.onGet(url, new Callback() {
//            @Override
//            public void onFailure(Request request, IOException e) {
//
//            }
//
//            @Override
//            public void onResponse(Response response) throws IOException {
//                String jsonString = GsonTool.getGosnString(response.body().byteStream());
//                Log.i("app1","getToken---->"+jsonString);
//                com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(jsonString);
//                Message message = new Message();
//                message.obj = jsonObject;
//                handler_deal.sendMessage(message);
//
//            }
//        });
//
//    }

}